package org.sample.dataconsumer.controller;

import org.sample.dataconsumer.client.UserFeignClient;
import org.sample.dataconsumer.config.ApplicationConfig;
import org.sample.dataconsumer.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

	private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private UserFeignClient client;

	@Autowired
	private LoadBalancerClient loadBalancerClient;

	@GetMapping("/{id}")
	public User getUser(@PathVariable("id") String id) {

		return client.getUser(id);
	}

	@GetMapping("/instance")
	public void logUserInstance() {
		ServiceInstance serviceInstance = loadBalancerClient.choose(ApplicationConfig.DATA_PROVIDER_SERVICE_NAME);
		LOGGER.info("ID: {}, host: {}, port: {}", serviceInstance.getServiceId(),
				serviceInstance.getHost(), serviceInstance.getPort());
	}

}
